<template>
  <el-drawer :title="title" :visible.sync="visible" append-to-body class="drawer-container" size="400px">
    <el-form ref="form" :model="formdata" :rules="formrule" label-position="right" label-suffix=":" label-width="100px">
      <el-form-item label="客户端编码" prop="code">
        <el-input v-model="formdata.code" :disabled="formdata.id != null" placeholder="请输入客户端编码"/>
      </el-form-item>
      <el-form-item label="客户端名称" prop="name">
        <el-input v-model="formdata.name" :disabled="formdata.id != null" placeholder="请输入客户端名称"/>
      </el-form-item>
      <el-form-item label="授权类型" prop="grantTypes">
        <el-select v-model="formdata.grantTypes" multiple placeholder="请输入授权类型" style="width: 100%">
          <el-option v-for="dict in localDict.grant_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="formdata.deviceType" placeholder="请输入设备类型" style="width: 100%">
          <el-option v-for="dict in localDict.device_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="登录验证码" prop="captcha">
        <el-select v-model="formdata.captcha" placeholder="请输入设备类型" style="width: 100%">
          <el-option v-for="dict in localDict.captcha_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item prop="activeTimeout" label-width="120px">
        <template slot="label">
            <span>
              <el-tooltip content="指定时间无操作则过期（单位：秒），默认30分钟（1800秒）" placement="top">
                <el-icon class="el-icon-warning"/>
              </el-tooltip>
              活跃超时时间
            </span>
        </template>
        <el-input-number v-model="formdata.activeTimeout" placeholder="请输入Token活跃超时时间" style="width: 100%"/>
      </el-form-item>
      <el-form-item prop="timeout" label-width="120px">
        <template #label>
            <span>
              <el-tooltip content="指定时间token必定过期（单位：秒），默认七天（604800秒）" placement="top">
                <el-icon class="el-icon-warning"/>
              </el-tooltip>
              固定超时时间
            </span>
        </template>
        <el-input-number v-model="formdata.timeout" placeholder="请输入Token固定超时时间" style="width: 100%"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formdata.status">
          <el-radio-button v-for="dict in localDict.system_status" :key="dict.value" :label="dict.value">
            {{ dict.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="注册开关" prop="register">
        <el-switch v-model="formdata.register" active-text="允许注册" inactive-text="禁止注册"/>
      </el-form-item>
      <el-form-item label="客户端描述" prop="remark">
        <el-input v-model="formdata.remark" placeholder="请输入客户端描述" rows="3" type="textarea"/>
      </el-form-item>
    </el-form>
    <div class="drawer-footer">
      <el-button size="mini" :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
      <el-button size="mini" @click="handleCancel">取 消</el-button>
    </div>
  </el-drawer>
</template>

<script>

import {getOne, save} from "@/api/base";

export default {
  name: "ClientModel",
  components: {},
  data() {
    return {
      title: '',
      visible: false,
      loading: false,
      baseUrl: '/system/client',
      formdata: {},
      formrule: {
        // 表单校验
        code: [
          {required: true, message: "客户端编码不能为空", trigger: "blur"},
          {pattern: /^\w{2,30}$/, message: '客户端编码只能包含数字、字母、下划线,长度在2-30个字符', trigger: 'blur'},
        ],
        name: [
          {required: true, message: "客户端名称不能为空", trigger: "blur"}
        ],
        deviceType: [
          {required: true, message: "设备类型不能为空", trigger: "blur"},
        ],
        grantTypes: [
          {required: true, message: "授权方式不能为空", trigger: "blur"},
        ],
        status: [
          {required: true, message: "客户端状态不能为空", trigger: "blur"}
        ],
      },
      localDict: {
        grant_type: [],
        device_type: [],
        captcha_type: [],
        system_status: []
      },
    }
  },
  created() {
    this.getLocalDict();
  },
  methods: {
    add() {
      this.resetForm('form')
      this.formdata = {
        status: '00'
      }
      this.visible = true
      this.title = '新增客户端'
    },
    edit(id) {
      this.resetForm('form')
      getOne(this.baseUrl, id).then(res => {
        this.formdata = res.data
        this.visible = true
        this.title = '修改客户端'
      })
    },
    async getLocalDict() {
      await this.getStaticData('system-device-type').then(res => {
        this.localDict.device_type = res.data
      })
      await this.getStaticData('system-grant-type').then(res => {
        this.localDict.grant_type = res.data
      })
      await this.getStaticData('system-captcha-type').then(res => {
        this.localDict.captcha_type = res.data
      })
      await this.getStaticData('system-status').then(res => {
        this.localDict.system_status = res.data
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          save(this.baseUrl, this.formdata).then(() => {
            this.$modal.msgSuccess("保存成功");
            this.visible = false;
            this.resetForm('form')
            this.$emit('ok')
          }).finally(() => {
            this.loading = false
          });
        }
      });
    },
    handleCancel() {
      this.visible = false
      this.resetForm('form')
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
